package jblip.apps.clip.cmds;

import java.util.Enumeration;

import jblip.BlipClient;
import jblip.apps.clip.ClipPrinter;
import jblip.resources.Subscription;

public class Subs extends ClipCommand {

  @Override
  public int getRequiredArgsCount() {
    return 0;
  }

  @Override
  public int getMaximumArgsCount() {
    return 2;
  }

  @Override
  public void execute(BlipClient client, String[] args) {
    String subs_user = client.getLoggedUserName();
    String type = "all";
    if (args.length > 0) {
      subs_user = args[args.length - 1];
    }
    if (args.length == 2) {
      type = args[0];
    }
    
    Enumeration<? extends Subscription> subs = null;
    if (type.equals("from")) {
      subs = client.getSubscriptionsFrom(subs_user);
    } else if (type.equals("to")) {
      subs = client.getSubscriptionsTo(subs_user);
    } else if (type.equals("all")) {
      subs = client.getSubscriptions(subs_user);
    } else {
      System.out.println("Unkown type: " + type);
      return;
    }
    
    
    if (client.getLastError() == null) {
      while (subs.hasMoreElements()) {
        ClipPrinter.print(subs.nextElement());
      }
    } else {
      System.out.println("ERROR: " + client.getLastError().toString());
    }
  }

  @Override
  public String getName() {
    return "subs";
  }

  @Override
  public String getUsageMessage() {
    return "[[all|from|to] nickname]";
  }

}
